<template>
  <div>
    <!--    添加权限-->
    <div id="form">
      <el-form label-width="280px" :model="tableData">
        <el-form-item label="权限标识">
          <el-input v-model="tableData.tag" placeholder="请输入权限标识字符串" style="width:100% "></el-input>
        </el-form-item>
        <el-form-item label="权限描述" style="margin-top: 50px">
          <el-input type="textarea" v-model="tableData.descp" placeholder="请输入权限描述"></el-input>
        </el-form-item>
        <el-form-item  label="父权限" >
          <el-select v-model="tableData.parent_id" filterable placeholder="请选择权限(不选默认为顶级权限)" style="width: 100%">
            <el-option
              v-for="item in permissionList"
              :key="item.id"
              :value="item.tag">
              <span style="float: left">{{ item.tag }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.descp }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-top: 50px;">
          <el-button style="margin-right: 30px" type="primary" @click="onSubmit">立即添加</el-button>
          <el-button type="info" @click="onClean">重新输入</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import permissionApi from '@/api/permission'
import qs from "qs";
export default {
  name: "add",
  data() {
    return {
      tableData: {
        tag: "",
        descp: "",
        parent_id:'',
      },
      permissionList:[]
    }
  },
  methods: {
    //查询所有权限
    selectAllPermission(){
      permissionApi.selectAll().then(resp=>{
        this.permissionList=resp.data;
      }).catch(()=>{
        this.$message.error('查询权限列表出错')
      })
    },
    //点击添加
    onSubmit() {
      if (!this.tableData.parent_id){
        this.tableData.parent_id=0;
      }
      permissionApi.addPermission(qs.stringify(this.tableData)).then(resp => {
        if (resp.code === 6666) {
          this.$message.success("添加成功！");
          this.onClean();
          this.$router.push('/rbac/permission/list')
        } else {
          this.$message.info(resp.msg)
        }
      }).catch(() => {
        this.$message.error("添加失败！")
      })
    },
    //清楚已输入的数据
    onClean() {
      this.tableData = {}
    }
  },
  mounted() {
    this.selectAllPermission();
  }
}
</script>

<style scoped>
#form {
  width: 680px;
  height: 400px;
  margin-top: 120px;
}
</style>
